package com.xp.code.daoImpl;

import java.sql.CallableStatement;
import java.sql.Connection;

import oracle.jdbc.driver.OracleTypes;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.xp.code.dao.IUserInfoDao;
import com.xp.code.jpa.DefaultJPADAO;
import com.xp.code.util.CommonUtils;
import com.xp.code.vo.UserInfoVO;

/**
 * 
 * @author HAHA
 * 
 */
@Repository("userInfoDao")
public class UserInfoDao extends DefaultJPADAO implements IUserInfoDao {

	public UserInfoDao() {

	}

	/*
	 * public int addUserInfo(UserInfoVO userInfoVO)throws Exception{ String
	 * sql=
	 * "INSERT INTO TSYS_USER(ID, VARNAME, PASSWORD, USERNAME, SEX, AGE, MOBILE, PHONE, VARMAIL, ADDRESS, CREATEDATE, VALIDDATE, VALIDFLAG, REMARK) VALUES(:ID, :VARNAME, :PASSWORD, :USERNAME, :SEX, :AGE, :MOBILE, :PHONE, :VARMAIL, :ADDRESS, SYSDATE, :VALIDDATE, :VALIDFLAG, :REMARK)"
	 * ; Query query=this.getEntityManager().createNativeQuery(sql);
	 * query.setParameter("ID", CommonUtils.getUUID());
	 * query.setParameter("VARNAME", userInfoVO.getVarname());
	 * query.setParameter("PASSWORD", userInfoVO.getPassword());
	 * query.setParameter("USERNAME", userInfoVO.getUsername());
	 * query.setParameter("SEX", userInfoVO.getSex()); query.setParameter("AGE",
	 * userInfoVO.getAge()); query.setParameter("MOBILE",
	 * userInfoVO.getMobile()); query.setParameter("PHONE",
	 * userInfoVO.getPhone()); query.setParameter("VARMAIL",
	 * userInfoVO.getVarmail()); query.setParameter("ADDRESS",
	 * userInfoVO.getAddress()); query.setParameter("VALIDDATE",
	 * userInfoVO.getValiddate()); query.setParameter("VALIDFLAG",
	 * userInfoVO.getValidflag()); query.setParameter("REMARK",
	 * userInfoVO.getRemark()); return query.executeUpdate(); }
	 * 
	 * public int isExistsUserByVarname(String varname)throws Exception{ String
	 * sql="SELECT VARNAME FROM TSYS_USER WHERE VARNAME=:VARNAME"; Query
	 * query=this.getEntityManager().createNativeQuery(sql);
	 * query.setParameter("VARNAME", varname); return
	 * query.getResultList().size(); }
	 */

	public UserInfoVO addUserInfos(UserInfoVO userInfoVO) throws Exception {
		UserInfoVO vo=null;
		CallableStatement cs = null;
		int FLAG = 0;
		String MSG = "添加成功";
		try {
			Session session = (Session) this.getEntityManager().getDelegate();
			Connection con = session.connection();
			cs = con.prepareCall("{CALL PACKAGEXPSYS_AUTH.AddUserInfo(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
			cs.setString(1, CommonUtils.getUUID());
			cs.setString(2, userInfoVO.getVarname());
			cs.setString(3, userInfoVO.getPassword());
			cs.setString(4, userInfoVO.getUsername());
			cs.setString(5, userInfoVO.getSex());
			cs.setInt(6, userInfoVO.getAge());
			cs.setInt(7, userInfoVO.getMobile());
			cs.setString(8, userInfoVO.getPhone());
			cs.setString(9, userInfoVO.getVarmail());
			cs.setString(10, userInfoVO.getAddress());
			cs.setString(11, userInfoVO.getValiddate());
			cs.setString(12, userInfoVO.getValidflag());
			cs.setString(13, userInfoVO.getRemark());
			cs.registerOutParameter(14, OracleTypes.INTEGER);
			cs.registerOutParameter(15, OracleTypes.VARCHAR);
			cs.executeUpdate();
			FLAG = cs.getInt(14);
			MSG = cs.getString(15);
			vo=new UserInfoVO(FLAG,MSG);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cs != null) {
				cs.close();
			}
		}
		return vo;
	}
}
